import lazyImg from "./lazyImg";

const blank = document.documentElement || document.body;
let bodyHeight = blank.clientHeight;
let swiperAll = null, swiperStart = false, fontSize;
var t, time, lazy, settime;
//
// document.addEventListener("touchmove", function(e) {    //防止向上滚动出现的bug
//     clearInterval(time);
//     time = null;
// },true);

function startImgLazy() {
    lazyImg.lazyImg(true);
    lazy = true;
}

function setScroll (scroll, pulldown, num, swiper) {
    clearTimeout(settime);
    settime = setTimeout(() => {
        if(num >= 0) {
            if (document.documentElement) {
                document.documentElement.scrollTop = document.body.scrollTop = num || 0;
            } else if (document.body) {
                document.body.scrollTop = num || 0;
            }
        } else {
            if (document.documentElement) {
                document.documentElement.scrollTop = document.body.scrollTop = scroll.top || 0;
            } else if (document.body) {
                document.body.scrollTop = scroll.top || 0;
            }
        }
        clearTimeout(settime);
        settime = null;

        if(swiper) {
            swiperAll = swiper;
            swiperAll.detachEvents();  //先清除
            swiperAll.attachEvents();   //再添加
            swiperAll.autoplay.start();
            swiperStart = true;
        }
        // window.onscroll = function() {
        //     onScroll(scroll, pulldown);
        // }
        //使用节流函数
        window.addEventListener('scroll',throttle(onScroll,10,50,scroll, pulldown));
        // window.addEventListener('scroll',throttle(onScroll,10,50,scroll, pulldown));

        fontSize = document.documentElement.style.fontSize;
        fontSize = fontSize.substring(0, fontSize.length-2);
    });


}

// 简单的节流函数
//fun 要执行的函数
//delay 小于time时间，延迟delay毫秒
//time  大于time时间，必须执行一次
var returnfun;
function throttle(fun, delay, time, scroll, pulldown) {
    var timeout,
        // startTime = new Date();
        startTime = 0;
    returnfun = function() {
        var context = this,      //this.指向window
            // args = arguments,
            curTime = new Date();
        // console.log("看看args");
        // console.log(args);
        // console.log(scroll2);
        clearTimeout(timeout);
        // 如果达到了规定的触发时间间隔，触发 handler
        if (curTime - startTime >= time) {
            // fun.apply(context, args);
            fun.call(context, scroll, pulldown);
            startTime = curTime;
            // 没达到触发间隔，重新设定定时器
        } else {
            timeout = setTimeout(function(){
                // fun.apply(context, args);
                fun.call(context, scroll, pulldown);
            }, delay);
        }
    };
    return returnfun;
};

function onScroll(scroll,pullDown) {
    // if(lazy) {
    lazyImg.lazyImg();
    // }
    if (document.documentElement) {
        t = document.documentElement.scrollTop + document.body.scrollTop;
    } else if (document.body) {
        t = document.body.scrollTop;
    }
    scroll.top = t;
    bodyHeight = bodyHeight ? bodyHeight : blank.clientHeight;   //处理安卓app的兼容
    if(swiperAll) {
        if(t >= fontSize *10 && swiperStart) {
            swiperAll.detachEvents();
            swiperStart = false;
        } else if(t < fontSize *10 && !swiperStart) {
            swiperAll.attachEvents();
            swiperAll.autoplay.start();
            swiperStart = true;
        } else {
        }
    }

    if (t > bodyHeight + 5) {
        scroll.backTop = true
    } else {
        scroll.backTop = false
    }
    if(pullDown) {
        var sumHeight = document.getElementById('app').clientHeight;
        if(sumHeight - bodyHeight * 2  - t < 0){
            pullDown();
        }
    }
}

var osTop, ispeed;
function up (scroll,e) {
    var event = e || window.event;
    event.stopPropagation();
    if (document.documentElement) {
        t = document.documentElement.scrollTop + document.body.scrollTop;
    } else if (document.body) {
        t = document.body.scrollTop;
    }

    // if(t > bodyHeight*3 ) {  //这里不动画，因为移动端卡
        if (document.documentElement) {
            document.documentElement.scrollTop = document.body.scrollTop = 0
        } else if (document.body) {
            document.body.scrollTop = 0
        }
        scroll.top = 0;

    // } else {
    //     time = setInterval(function(){
    //         osTop = (document.documentElement?document.documentElement.scrollTop : 0) + document.body.scrollTop;
    //         ispeed = Math.floor(-osTop/3);
    //         if (document.documentElement) {
    //             document.documentElement.scrollTop = document.body.scrollTop = osTop+ispeed;
    //         } else if (document.body) {
    //             document.body.scrollTop = osTop+ispeed;
    //         }
    //         // document.documentElement.scrollTop = document.body.scrollTop = osTop+ispeed;  //个人建议这种
    //         if(osTop <= 2){
    //             scroll.top = 0;
    //             if (document.documentElement) {
    //                 document.documentElement.scrollTop = document.body.scrollTop = 0;
    //             } else if (document.body) {
    //                 document.body.scrollTop = 0;
    //             }
    //             clearInterval(time);
    //             time = null;
    //         }
    //     },13);
    // }
    // scroll.backTop = false;
}

function scrollTo(num, e){
    var event = e || window.event;
    event.stopPropagation();
    if (document.documentElement) {
        document.documentElement.scrollTop = document.body.scrollTop = num || 0;
    } else if (document.body) {
        document.body.scrollTop = num || 0;
    }
}

function closeScroll () {
    // if (document.documentElement && document.documentElement.scrollTop) {
    //     scroll.top = document.documentElement.scrollTop;
    // } else if (document.body) {
    //     scroll.top = document.body.scrollTop;
    // }

    // if (document.documentElement && document.documentElement.scrollTop) {
    //     document.documentElement.scrollTop = 0
    // } else if (document.body) {
    //     document.body.scrollTop = 0
    // }
    // lazy = false;
    // window.onscroll = null
    if(swiperAll) {
        swiperAll.detachEvents();
        swiperStart = false;
        swiperAll = null;
    }

    window.removeEventListener('scroll',returnfun);
}
function toTop(){
    if (document.documentElement) {
        document.documentElement.scrollTop = document.body.scrollTop = 0
    } else if (document.body) {
        document.body.scrollTop = 0
    }
}


export default {
    setScroll: setScroll,
    closeScroll: closeScroll,
    up: up,
    clearScroll: up,
    startImgLazy:startImgLazy,
    scrollTo:scrollTo,
    toTop:toTop
}
